/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author Administrator
 */
public class HttpClientUt {
     public static final int cache = 10 * 1024;  

    public static String httpPost(String url, List<NameValuePair> list) {
        String result = null;
        try {
            // (1) Post请求
            HttpPost post = new HttpPost(url);

//            List<NameValuePair> params = new ArrayList<NameValuePair>();  
//params.add(new BasicNameValuePair("name", "ahopedog"));  
//params.add(new BasicNameValuePair("work", "程序员"));  
//            
            post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
             CloseableHttpClient http = null;
              CloseableHttpResponse response = null;
            try {
                // (3) 发送请求
                 http = HttpClients.createDefault();
                 response = http.execute(post);

                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    result = EntityUtils.toString(entity, HTTP.UTF_8);

                    System.out.println("-----------------postresult:" + result);

                    return result;
                }
            } finally {
                response.close();
                http.close();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }
    
    /**
     * 上传文件
     * 
     * @param filePath
     * @param url
     * @return
     */
    public static String postFile(String filePath, String url){
        System.out.println("0999999999999999"+url);
        HttpClient httpclient = new DefaultHttpClient();
        //设置通信协议版本
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
         
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity mpEntity = new MultipartEntity(); //文件传输
        ContentBody cbFile = new FileBody(new File(filePath));
               
        mpEntity.addPart("file", cbFile); // <input type="file" name="file" />  对应的
        httpPost.setEntity(mpEntity);
         
        HttpResponse httpResponse = null;
        HttpEntity resEntity = null;
        String result = null;
		try {
			httpResponse = httpclient.execute(httpPost);
			resEntity = httpResponse.getEntity();
			if (httpResponse.getStatusLine().getStatusCode()==200){
				result = EntityUtils.toString(resEntity, "utf-8");
	        }
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (resEntity != null) {
	            try {
					resEntity.consumeContent();
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
	     
	        httpclient.getConnectionManager().shutdown();
		}
        
        return result;
      } 


 public static String download(String filepath, String url) {  
        try {  
            HttpClient client = new DefaultHttpClient();  
            HttpGet httpget = new HttpGet(url);  
            HttpResponse response = client.execute(httpget);  
  
            HttpEntity entity = response.getEntity();  
            InputStream is = entity.getContent();  
//            if (filepath == null)  
//                filepath = getFilePath(response);  
            File file = new File(filepath);  
            file.getParentFile().mkdirs();  
            FileOutputStream fileout = new FileOutputStream(file);  
            /** 
             * 根据实际运行效果 设置缓冲区大小 
             */  
            byte[] buffer=new byte[cache];  
            int ch = 0;  
            while ((ch = is.read(buffer)) != -1) {  
                fileout.write(buffer,0,ch);  
            }  
            is.close();  
            fileout.flush();  
            fileout.close();  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  


}
